package com.example.hcd.functionactivity;

import java.util.ArrayList;

import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.example.hcd.R;
import com.example.hcd.datamanage.ImageAndText;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Administrator
 *
 */
public class ShowPictureList extends BaseActivity implements OnPageChangeListener
{
	// 图片地址集
	ArrayList<String> picPathList = new ArrayList<String>();
	// 是否是本地图片
	private boolean isbendi;
	// viewpage
	private ViewPager viewPager;

	// 显示默认位置
	private int moreIndex;

	
	private TextView pageText;
	private int window_width;
	private int window_height;



	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mPageName = "ShowPictureList";
		setContentView(R.layout.show_pic_viewpage);

		picPathList = getIntent().getStringArrayListExtra("PICK_PATH_LIST");
		isbendi = getIntent().getBooleanExtra("IS_BENDI", isbendi);
		moreIndex = getIntent().getIntExtra("MOREN_INDEX", moreIndex);

		ImageView imageView =(ImageView)findViewById(R.id.iv_back);
		imageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		ImageView rotata_iv =(ImageView)findViewById(R.id.iv_rotate);
		rotata_iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SubsamplingScaleImageView view =null;
				if(viewPager.getAdapter() instanceof ImageAdapterBendi)
				{
					view =((SubsamplingScaleImageView)((ImageAdapterBendi)viewPager.getAdapter()).getCurrentView().getChildAt(0));
				}else if(viewPager.getAdapter() instanceof ImageAdapter)
				{
					view =((SubsamplingScaleImageView)((ImageAdapter)viewPager.getAdapter()).getCurrentView().getChildAt(0));
				}
                if(view!=null)
				{
					switch (view.getOrientation())
					{
						case SubsamplingScaleImageView.ORIENTATION_USE_EXIF:
							view.setOrientation(SubsamplingScaleImageView.ORIENTATION_0);
							break;
						case SubsamplingScaleImageView.ORIENTATION_0:
							view.setOrientation(SubsamplingScaleImageView.ORIENTATION_90);
							break;
						case SubsamplingScaleImageView.ORIENTATION_90:
							view.setOrientation(SubsamplingScaleImageView.ORIENTATION_180);
							break;
						case SubsamplingScaleImageView.ORIENTATION_180:
							view.setOrientation(SubsamplingScaleImageView.ORIENTATION_270);
							break;
						case SubsamplingScaleImageView.ORIENTATION_270:
							view.setOrientation(SubsamplingScaleImageView.ORIENTATION_0);
							break;
						default:
							view.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
							break;
					}


				}
			}
		});

		viewPager = (ViewPager) findViewById(R.id.view_pager);


		if (isbendi)
		{
			viewPager.setAdapter(new ImageAdapterBendi(picPathList));

		} else
		{
			viewPager.setAdapter(new ImageAdapter(picPathList));
		}
		viewPager.setCurrentItem(moreIndex);

		viewPager.setOnPageChangeListener(this);

		// progressBar = (ProgressBar) findViewById(R.id.loading);

		pageText = (TextView) findViewById(R.id.page_text);

		// 设定当前的页数和总页数
		pageText.setText((moreIndex + 1) + "/" + picPathList.size());

		/* 获取可見区域高度 */
		WindowManager manager = getWindowManager();
		window_width = manager.getDefaultDisplay().getWidth();
		window_height = manager.getDefaultDisplay().getHeight();

	}

	@SuppressWarnings("unused")
	private class ImageAdapter extends PagerAdapter
	{

		private LayoutInflater inflater;
		private ArrayList<ImageAndText> list = new ArrayList<ImageAndText>();
		private ViewGroup rootView;
		/* 加载图片 */
		// ImageLoaderUtil imageUtil;

		ImageAdapter(ArrayList<String> list) {
			inflater = LayoutInflater.from(ShowPictureList.this);
			// imageUtil = new ImageLoaderUtil(ShowPictureList.this);
			for (int i = 0; i < list.size(); i++)
			{
				ImageAndText image = new ImageAndText(list.get(i), list.get(i));
				this.list.add(image);
			}
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object)
		{
			container.removeView((View) object);
		}

		@Override
		public int getCount()
		{
			return list.size();
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position)
		{

			View itemView = LayoutInflater.from(ShowPictureList.this).inflate(R.layout.pager_gallery_item, container, false);

			container.addView(itemView);
			final SubsamplingScaleImageView iv =
					(SubsamplingScaleImageView) itemView.findViewById(R.id.image);
			iv.setMaxScale(3.0f);
			iv.setMinScale(0.5f);
			iv.setTag(position);
//            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
//            ImageLoader.getInstance().displayImage(imgUrlList.get(position), iv);

			if (list.get(position).getPicbitMap() != null)
			{
				iv.setImage(ImageSource.bitmap(list.get(position).getPicbitMap()));
			} else
			{
				try {
					iv.setImage(ImageSource.bitmap(ImageLoader.getInstance().loadImageSync(list.get(position).getImageUrl())));
				}catch(Exception e)
				{

				}

			}

			return itemView;
		}
		public void setPrimaryItem(ViewGroup container, int position, Object object) {
			rootView = (ViewGroup) object;

			super.setPrimaryItem(container, position, object);
		}

		public ViewGroup getCurrentView()
		{
			return rootView;
		}


		@Override
		public boolean isViewFromObject(View view, Object object)
		{
			return view.equals(object);
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader)
		{
		}

		@Override
		public Parcelable saveState()
		{
			return null;
		}
	}

	@SuppressWarnings("unused")
	private class ImageAdapterBendi extends PagerAdapter
	{

		private LayoutInflater inflater;
		private ArrayList<String> list;
		private ViewGroup rootView;

		ImageAdapterBendi(ArrayList<String> list) {
			inflater = LayoutInflater.from(ShowPictureList.this);
			this.list = list;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object)
		{
			container.removeView((View) object);
		}

		@Override
		public int getCount()
		{
			return list.size();
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position)
		{
			View itemView = LayoutInflater.from(ShowPictureList.this).inflate(R.layout.pager_gallery_item, container, false);

			container.addView(itemView);
			final SubsamplingScaleImageView iv =
					(SubsamplingScaleImageView) itemView.findViewById(R.id.image);
			iv.setMaxScale(3.0f);
			iv.setMinScale(0.5f);
			iv.setTag(position);
//            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
//            ImageLoader.getInstance().displayImage(imgUrlList.get(position), iv);
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 1;
			Bitmap bitmap = BitmapFactory.decodeFile(list.get(position), options);
			if (bitmap != null)
			{
				iv.setImage(ImageSource.bitmap(bitmap));

			} else
			{
				iv.setImage(ImageSource.resource(R.drawable.image));
			}


			return itemView;
		}

		@Override
		public void setPrimaryItem(ViewGroup container, int position, Object object) {
			rootView = (ViewGroup) object;

			super.setPrimaryItem(container, position, object);
		}

		public ViewGroup getCurrentView()
		{
			return rootView;
		}

		@Override
		public boolean isViewFromObject(View view, Object object)
		{
			return view.equals(object);
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader)
		{
		}




		@Override
		public Parcelable saveState()
		{
			return null;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0)
	{
		// TODO Auto-generated method stub
		// 每当页数发生改变时重新设定一遍当前的页数和总页数
		pageText.setText((arg0 + 1) + "/" + picPathList.size());
	}

}
